/*************************************************************************
	> File Name: ConPro.hpp
	> Author: H.M. Lau
	> Mail: hm.lau@outlook.com
	> Created Time: 2023/09/26 10:06
 ************************************************************************/

#include "comm.hpp"
#include "BlockQueue.hpp"

int add(int x, int y)
{
    return x + y;
}
void* productor(void *args)
{
    BlockQueue<DATA> *bqueue = (BlockQueue<DATA>*)args;
    while (1) {
        int x = rand() % 500 + 1;
        int y = rand() % 1000 + 2;
        DATA d(x, y, add);
        bqueue->push(d);
        std::cout << pthread_self() << " productor have produced x:" << d.getX() << " y:" << d.getY() << std::endl;
        sleep(1);
    }
    return nullptr;
}

void* consumer(void *args)
{
    BlockQueue<DATA> *bqueue = (BlockQueue<DATA>*)args;
    while (1) {
        DATA d;
        bqueue->pop(&d);
        std::cout << pthread_self() << " consumer get x:" << d.getX() << " y:" << d.getY() << ", processed the result:" << d() << std::endl;
        sleep(1);
    }
    return nullptr;
}
